################################
# 07_prepareRollCallMexico-1.R #
################################

rm(list=ls())
print (version)

# platform       aarch64-apple-darwin20      
# arch           aarch64                     
# os             darwin20                    
# system         aarch64, darwin20           
# status                                     
# major          4                           
# minor          1.2                         
# year           2021                        
# month          11                          
# day            01                          
# svn rev        81115                       
# language       R                           
# version.string R version 4.1.2 (2021-11-01)
# nickname       Bird Hippie          

library (car)
library (runjags)
library (coda)
library (openxlsx)
library (reshape2)
library (random)
library (scales)
library (withr)
library (stringr)


## This file takes information on rollcall and party pressure data from Mexico
##        sets up jags data that eliminates deputies
##        that were not yet in Congress

# Set working directory
savePathRep <- "/all_data/"
setwd (savePathRep)


anyo <- c(1,2,3)
sess <- c(1,2)

#### Gather data ####
## Read roll call object 
# rollCallData contains exclusively votes that are divisive, which are the ones we analyze
rollCallData <- read.table (file="mexico_original_rollcall.txt"
                        , sep="\t", header=T, row.names = "legislator.no")
included.votes <- colnames (rollCallData)

# realRollCall contains deputy names and ALL votes, even non-divisive ones
# we need to keep the deputy names, but we will constrain realRollCall to have
# only the necessary votes
realRollCall <- read.table ("mexico_rollcall_with_names.txt"
                            , sep="\t", header=T)
realRollCall.ltd <- realRollCall[,4:ncol(realRollCall)]

realRollCall.ltd <- realRollCall.ltd[,is.element (colnames (realRollCall.ltd), included.votes)]
realRollCall.ltd <- data.frame (cbind (realRollCall[,1:3], realRollCall.ltd))

## Read auxiliary information about legislators
LM.info <- read.table("mexico_id_information_1.txt", header=TRUE, sep="\t")
uniqueID <- as.character (LM.info$uniqueID)
initial.party <- as.character (LM.info$party)
initial.party.num <- car::recode (initial.party, "'PRI'=1
                          ; 'PAN'=2
                          ; 'PRD'=3
                          ; 'PVEM'=4
                          ; 'PANAL'=5
                          ; 'MC'=6
                          ; 'PT'=7
                          ; 'SP'=8") # SP: sin partido

## Read full party info
LM.info.full <- read.table("mexico_id_information_2.txt", header=TRUE, sep="\t")


## Read vote-specific party information about legislators
## (many of them switched throughout their term)
complete.party <- read.table (file="mexico_party_membership.txt", header=T, sep="\t")

# Change colnames of complete.party (remove "party")
colnames (complete.party) <- gsub ("party", "", colnames(complete.party))

# Check that all rollcallvotes appear in the complete.party matrix (sum should be 0)
sum (!is.element (colnames(rollCallData), colnames(complete.party)))

# Keep only columns in complete.party for which we have vote information
complete.party.ltd <- data.frame (cbind (complete.party[,1:2], complete.party[,is.element(colnames(complete.party), colnames(rollCallData))]))

# Figure out which legislators in complete.party.ltd do not appear in rollcallvotes
complete.party.ltd$names[!is.element (complete.party.ltd$uniqueID, uniqueID)]

# we only need information about district, and districtType, district.no, and state to be copied from LM.info to LM.info.full
dist.info <- LM.info[,is.element(colnames(LM.info), c("names","districtType","district","gender","state","district.no"))]
LM.info.full <- base::merge (LM.info.full, dist.info, by="names", all.x=T)
LM.info.full <- LM.info.full[!duplicated(LM.info.full),] # eliminate duplicates
LM.info.full$names[is.na(LM.info.full$districtType)] # Deputies lacking district info, but most of them do not appear in the LXI legislature

# Merge
complete.party.ltd$id <- paste (complete.party.ltd$names, complete.party.ltd$uniqueID, sep="-")
LM.info.full$id <- paste (LM.info.full$names, LM.info.full$uniqueID, sep="-")
complete.party.ltd <- base::merge (complete.party.ltd, LM.info.full, by="id", all.x=T) 
complete.party.ltd <- complete.party.ltd[, -grep ("names.y", colnames(complete.party.ltd))]
complete.party.ltd <- complete.party.ltd[, -grep ("uniqueID.y", colnames(complete.party.ltd))]
colnames (complete.party.ltd)[grep("names.x", colnames(complete.party.ltd))] <- "names"
colnames (complete.party.ltd)[grep("uniqueID.x", colnames(complete.party.ltd))] <- "uniqueID"


## Add 41 legislators to the .txt version of legislatorsMex61-Final that were originally missing
new.info.leg <- read.table("mexico_deputy_information.txt", header=TRUE, sep="\t")

complete.party.ltd$names[!is.element(complete.party.ltd$names, new.info.leg$Deputy)]

complete.party.ltd <- merge (x=complete.party.ltd, y=new.info.leg, by.x="names", by.y="Deputy"
                             , all.x=TRUE, all.y=FALSE)

# Remove duplicates of deputies
complete.party.ltd <- complete.party.ltd[!duplicated(complete.party.ltd),]

realRollCall.ltd$id <- paste (realRollCall.ltd$names, realRollCall.ltd$uniqueID, sep="-")

# Sort both datasets on ascending values of "id"
realRollCall.ltd <- realRollCall.ltd[with(realRollCall.ltd, order(id)), ]
complete.party.ltd <- complete.party.ltd[with(complete.party.ltd, order(id)), ]

####################################################################################
# for further information on licencias e incorporaciones, take a look here:
# http://sitl.diputados.gob.mx/LXI_leg/movimientos_de_diputadosnplxi.php?pert=12
# Info on districts: https://es.wikipedia.org/wiki/LXI_Legislatura_del_Congreso_de_la_Uni??n_de_M??xico#Diputados_por_distrito_uninominal_(mayor??a_relativa)
# District info on missing legislatures is now in PartyEffects/Data/missingDeputies.txt
####################################################################################

## build matrices for analysis
quasi.full.party.membership <- complete.party.ltd[,grep("V", colnames (complete.party.ltd))]
quasi.full.roll.call  <- realRollCall.ltd[,grep("V", colnames (realRollCall.ltd))]

## Recode datasets
full.party.membership <- apply (quasi.full.party.membership, 2, car::recode, 
                                      "'PRI'=1
                                      ; 'PAN'=2
                                      ; 'PRD'=3
                                      ; 'MC'=6
                                      ; 'PVEM'=4
                                      ; 'PT'=7
                                      ; 'PANAL'=5
                                      ; 'SP'=8")

full.roll.call <- apply (quasi.full.roll.call, 2, car::recode,
                         "3=1; 6=0; 9=NA; 11=NA; -999=NA")
###############################################################################
# Vote codes
# 3 "a favor"
# 6 "en contra"
# 9 "se abstuvieron"
# 11 "presentes y no votaron"
# -999 "estuvieron ausentes"
# NA appears to be not in congress, but also not tagged as "estuvo ausente"
#     a few of this were suplentes not yet in congress
##############################################################################

# Import district information on the additional legislators
missing.district <- read.table ("mexico_missing_deputies.txt"
                                , header=T, sep="\t")

list.incompletes <- complete.party.ltd$names[is.na(complete.party.ltd$districtType)]
for (i in 1:length (list.incompletes)){
   dep <- list.incompletes[i]
   which.dep <- grep (dep, missing.district$names)
   print (which.dep)
   complete.party.ltd$districtType[complete.party.ltd$names==dep] <- missing.district$districtType[which.dep]
   complete.party.ltd$district[complete.party.ltd$names==dep] <- missing.district$district[which.dep]
   complete.party.ltd$state[complete.party.ltd$names==dep] <- missing.district$state[which.dep]
   complete.party.ltd$district.no[complete.party.ltd$names==dep] <- missing.district$district.no[which.dep]
   complete.party.ltd$gender[complete.party.ltd$names==dep] <- missing.district$gender[which.dep]
}
complete.party.ltd$district.type.r <- as.numeric (car::recode (complete.party.ltd$districtType, "'PR'=2; 'SMD'=1"))
complete.party.ltd$gender <- as.numeric (complete.party.ltd$gender)

## Add district-level information (margin of victory, first-loser party)
complete.party.ltd$state.abbrev <- car::recode (complete.party.ltd$state, 
                                           "'Aguascalientes'='AGS'
                                            ; 'Baja California'='BC'
                                            ; 'Baja California Sur'='BCS'
                                            ; 'Campeche'='CAM'
                                            ; 'Chiapas'='CHIA'
                                            ; 'Chihuahua'='CHUA'
                                            ; 'Coahuila'='CLA'
                                            ; 'Colima'='CMA'
                                            ; 'DF'='DF'
                                            ; 'Durango'='DGO'
                                            ; 'EdoMex'='EDOMEX'
                                            ; 'Guanajuato'='GTO'
                                            ; 'Guerrero'='GRO'
                                            ; 'Hidalgo'='HID'
                                            ; 'Jalisco'='JAL'
                                            ; 'Michoacan'='MICH'
                                            ; 'Morelos'='MOR'
                                            ; 'Nayarit'='NAY'
                                            ; 'Nuevo Leon'='NL'
                                            ; 'Oaxaca'='OAX'
                                            ; 'Puebla'='PUE'
                                            ; 'Queretaro'='QUER'
                                            ; 'Quintana Roo'='QROO'
                                            ; 'Region 1'='R1'
                                            ; 'Region 2'='R2'
                                            ; 'Region 3'='R3'
                                            ; 'Region 4'='R4'
                                            ; 'Region 5'='R5'
                                            ; 'San Luis Potosi'='SLP'
                                            ; 'Sinaloa'='SIN'
                                            ; 'Sonora'='SON'
                                            ; 'Tabasco'='TAB'
                                            ; 'Tamaulipas'='TAM'
                                            ; 'Tlaxcala'='TLAX'
                                            ; 'Veracruz'='VER'
                                            ; 'Yucatan'='YUC'
                                            ; 'Zacatecas'='ZAC'") 

complete.party.ltd$district.abbrev <- paste (complete.party.ltd$state.abbrev
                          , with_options(
                            c(scipen = 999)
                            , str_pad(complete.party.ltd$district.no, 2, pad = "0"))
                          , sep="_")

district.Information<-read.table("mexico_district_information.txt")

complete.party.ltd <- merge (complete.party.ltd, district.Information
                             , by.x="district.abbrev"
                             , by.y="district"
                             , all.x=TRUE
                             , all.y=FALSE)


# There are a number of legislators that vote very few times
# Eliminate those that vote less than 10 times
no.votes <- apply (full.roll.call, 1, function (x) length (x[!is.na(x)]))
length (no.votes[no.votes < 11]) # 47 people voted very few times


complete.party.ltd <- complete.party.ltd[no.votes>10,]
full.roll.call  <- full.roll.call[no.votes>10,]
full.party.membership <- full.party.membership[no.votes>10,]
deputy.names.roll.call  <- realRollCall.ltd$names[no.votes>10]   # complete.party.ltd
deputy.names.full.party <- complete.party.ltd$names #[no.votes>10]
# Keep a record of the names of individuals that are kept in full.party.membership
# and full.roll.call, as we will want to recover them

legislators.in.analysis <- complete.party.ltd[,c(1:3)] #[no.votes>10,c(1:3)]
# Which legislators are entered as two different people (under different parties)?
legislators.in.analysis$names[(duplicated(legislators.in.analysis$names))]

############################################################################################
## Figure out who really sits in legislature from overlaying RC and party.membership
############################################################################################
quasi.full.party.membership <- quasi.full.party.membership[no.votes>10,]
quasi.full.roll.call <- quasi.full.roll.call[no.votes>10,]
# Create a matrix of dimensions identical to quasi.full.roll.call and quasi.full.party.membership
# The matrix will have 0 if there is NA in both quasi.full.roll.call and quasi.full.party.membership
# and 1 otherwise
in.legislature <- matrix (NA, ncol=ncol(quasi.full.roll.call), nrow=nrow(quasi.full.roll.call))
for (i in 1:nrow(in.legislature)){
   for (j in 1:ncol(in.legislature)){
      in.legislature[i,j] <- ifelse (is.na(quasi.full.roll.call[i,j]) & is.na(quasi.full.party.membership[i,j]), 0, 1)
   }
}


out.legislature <- matrix (NA, ncol=ncol(quasi.full.roll.call), nrow=nrow(quasi.full.roll.call))
for (i in 1:nrow(out.legislature)){
  for (j in 1:ncol(out.legislature)){
    out.legislature[i,j] <- ifelse (is.na(quasi.full.roll.call[i,j]) & !is.na(quasi.full.party.membership[i,j]), 1, 0)
  }
}
length (rowSums (out.legislature)[rowSums (out.legislature) != 0])
# 41 legislators have a party code, but were not in Congress at the time 

complete.party.ltd$state.muni.leg <- ifelse (complete.party.ltd$State.Legislator==1 | 
                                               complete.party.ltd$Municipal.Legislator==1, 1, 0)
complete.party.ltd$executive.exp <- ifelse (complete.party.ltd$Mayor==1 | complete.party.ltd$Governor==1
                                            | complete.party.ltd$Senator==1, 1, 0)


####################################################################################
## Import pressure data
####################################################################################

## Build matrices that will contain the PR legislators of each party.
PAN.PR.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRI.PR.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRD.PR.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PAN.SMD.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRD.SMD.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRI.SMD.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PAN.member.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRI.member.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRD.member.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
Year.1.vote.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
Year.2.vote.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
Year.3.vote.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PAN.commPres.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRI.commPres.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRD.commPres.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PAN.cabinet.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRI.cabinet.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRD.cabinet.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PAN.state.muni.leg.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRI.state.muni.leg.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRD.state.muni.leg.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PAN.exec.exp.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRI.exec.exp.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRD.exec.exp.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))

for (i in 1:nrow(PAN.PR.matrix)){
   for (j in 1:ncol(PAN.PR.matrix)){
      PAN.PR.matrix[i,j] <- ifelse (complete.party.ltd$district.type.r[i]==2 
                                & full.party.membership[i,j]==2
                                & !is.na(full.party.membership[i,j]), 1, 0)
      PRI.PR.matrix[i,j] <- ifelse (complete.party.ltd$district.type.r[i]==2 
                                 & full.party.membership[i,j]==1
                                 & !is.na(full.party.membership[i,j]), 1, 0)
      PRD.PR.matrix[i,j] <- ifelse (complete.party.ltd$district.type.r[i]==2 
                                 & full.party.membership[i,j]==3
                                 & !is.na(full.party.membership[i,j]), 1, 0)
      PAN.SMD.matrix[i,j] <- ifelse (complete.party.ltd$district.type.r[i]==1 
                                     & full.party.membership[i,j]==2
                                     & !is.na(full.party.membership[i,j]), 1, 0)
      PRI.SMD.matrix[i,j] <- ifelse (complete.party.ltd$district.type.r[i]==1 
                                    & full.party.membership[i,j]==1
                                    & !is.na(full.party.membership[i,j]), 1, 0)
      PRD.SMD.matrix[i,j] <- ifelse (complete.party.ltd$district.type.r[i]==1 
                                    & full.party.membership[i,j]==3
                                    & !is.na(full.party.membership[i,j]), 1, 0)
      PAN.member.matrix[i,j] <- ifelse (full.party.membership[i,j]==2
                                     & !is.na(full.party.membership[i,j]), 1, 0)
      PRI.member.matrix[i,j] <- ifelse (full.party.membership[i,j]==1
                                        & !is.na(full.party.membership[i,j]), 1, 0)
      PRD.member.matrix[i,j] <- ifelse (full.party.membership[i,j]==3
                                        & !is.na(full.party.membership[i,j]), 1, 0)
      Year.1.vote.matrix[i,j] <- ifelse (grepl (".1.", colnames(full.party.membership)[j], fixed=TRUE)==TRUE, 1, 0)
      Year.2.vote.matrix[i,j] <- ifelse (grepl (".2.", colnames(full.party.membership)[j], fixed=TRUE)==TRUE, 1, 0)
      Year.3.vote.matrix[i,j] <- ifelse (grepl (".3.", colnames(full.party.membership)[j], fixed=TRUE)==TRUE, 1, 0)
   }
}


for (i in 1:nrow(PAN.commPres.matrix)){
  for (j in 1:ncol(PAN.commPres.matrix)){
    PAN.commPres.matrix[i,j] <- ifelse (complete.party.ltd$Committee.President[i]==1
                               & !is.na(complete.party.ltd$Committee.President[i])
                               & full.party.membership[i,j]==2
                               & !is.na(full.party.membership[i,j]), 1, 0)
    PRI.commPres.matrix[i,j] <- ifelse (complete.party.ltd$Committee.President[i]==1
                               & !is.na(complete.party.ltd$Committee.President[i])
                               & full.party.membership[i,j]==1
                               & !is.na(full.party.membership[i,j]), 1, 0)
    PRD.commPres.matrix[i,j] <- ifelse (complete.party.ltd$Committee.President[i]==1
                               & !is.na(complete.party.ltd$Committee.President[i])
                               & full.party.membership[i,j]==3
                               & !is.na(full.party.membership[i,j]), 1, 0)
    PAN.cabinet.matrix[i,j] <- ifelse (complete.party.ltd$Cabinet.Position[i]==1
                              & !is.na(complete.party.ltd$Cabinet.Position[i])
                              & full.party.membership[i,j]==2
                              & !is.na(full.party.membership[i,j]), 1, 0)
    PRI.cabinet.matrix[i,j] <- ifelse (complete.party.ltd$Cabinet.Position[i]==1
                              & !is.na(complete.party.ltd$Cabinet.Position[i])
                              & full.party.membership[i,j]==1
                              & !is.na(full.party.membership[i,j]), 1, 0)
    PRD.cabinet.matrix[i,j] <- ifelse (complete.party.ltd$Cabinet.Position[i]==1
                              & !is.na(complete.party.ltd$Cabinet.Position[i])
                              & full.party.membership[i,j]==3
                              & !is.na(full.party.membership[i,j]), 1, 0)
    PAN.state.muni.leg.matrix[i,j] <- ifelse (complete.party.ltd$state.muni.leg[i]==1
                                     & !is.na(complete.party.ltd$state.muni.leg[i])
                                     & full.party.membership[i,j]==2
                                     & !is.na(full.party.membership[i,j]), 1, 0)
    PRI.state.muni.leg.matrix[i,j] <- ifelse (complete.party.ltd$state.muni.leg[i]==1
                                     & !is.na(complete.party.ltd$state.muni.leg[i])
                                     & full.party.membership[i,j]==1
                                     & !is.na(full.party.membership[i,j]), 1, 0)
    PRD.state.muni.leg.matrix[i,j] <- ifelse (complete.party.ltd$state.muni.leg[i]==1
                                     & !is.na(complete.party.ltd$state.muni.leg[i])
                                     & full.party.membership[i,j]==3
                                     & !is.na(full.party.membership[i,j]), 1, 0)
    PAN.exec.exp.matrix[i,j] <- ifelse (complete.party.ltd$executive.exp[i]==1
                               & !is.na(complete.party.ltd$executive.exp[i])
                               & full.party.membership[i,j]==2
                               & !is.na(full.party.membership[i,j]), 1, 0)
    PRI.exec.exp.matrix[i,j] <- ifelse (complete.party.ltd$executive.exp[i]==1
                               & !is.na(complete.party.ltd$executive.exp[i])
                               & full.party.membership[i,j]==1
                               & !is.na(full.party.membership[i,j]), 1, 0)
    PRD.exec.exp.matrix[i,j] <- ifelse (complete.party.ltd$executive.exp[i]==1
                               & !is.na(complete.party.ltd$executive.exp[i])
                               & full.party.membership[i,j]==3
                               & !is.na(full.party.membership[i,j]), 1, 0)
  }
}

# Build matrices for gender
PAN.fem.matrix  <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PAN.male.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRI.fem.matrix  <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRI.male.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRD.fem.matrix  <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRD.male.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
female.matrix   <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))

PAN.poverty.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRI.poverty.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRD.poverty.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PAN.victory.margin.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRI.victory.margin.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRD.victory.margin.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PAN.victory.margin.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRI.victory.margin.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRD.victory.margin.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PAN.competitorID.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRI.competitorID.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRD.competitorID.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PAN.year1.vote.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PAN.year2.vote.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PAN.year3.vote.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRI.year1.vote.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRI.year2.vote.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRI.year3.vote.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRD.year1.vote.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRD.year2.vote.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRD.year3.vote.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PAN.pcbrequest.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRI.pcbrequest.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRD.pcbrequest.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PAN.prezposition.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRI.prezposition.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRD.prezposition.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PAN.cabposition.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRI.cabposition.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRD.cabposition.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PAN.proceduralvote.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRI.proceduralvote.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRD.proceduralvote.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PAN.year.vote.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRI.year.vote.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PRD.year.vote.matrix <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))

committeeChair <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
stateLegislator <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PCB <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PrezPosition <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
Year <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
Income <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
PR <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))

victoryMargin    <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
victoryMarginDummy <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
proceduralVote   <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
cabinetExp  <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))
executiveExp     <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))

substituteLegs <- matrix (NA, ncol=ncol(full.roll.call), nrow=nrow(full.roll.call))

 MexBill61<- read.table("mexico_party_votes.txt")


# Because "V8.2.2" is not in full.roll.call, we will need to eliminate this column (element)
# from all of the datasets we have so far created
bad.vote <- grep ("V8.2.2", colnames (full.roll.call))
full.party.membership <- full.party.membership[,-bad.vote]
full.roll.call <- full.roll.call[,-bad.vote]
quasi.full.party.membership <- quasi.full.party.membership[,-bad.vote]
quasi.full.roll.call <- quasi.full.roll.call[,-bad.vote]
PAN.PR.matrix <- PAN.PR.matrix[,-bad.vote]
PRD.PR.matrix <- PRD.PR.matrix[,-bad.vote]
PRI.PR.matrix <- PRI.PR.matrix[,-bad.vote]
PRI.SMD.matrix <- PRI.SMD.matrix[,-bad.vote]
PRD.SMD.matrix <- PRD.SMD.matrix[,-bad.vote]
PAN.SMD.matrix <- PAN.SMD.matrix[,-bad.vote]
PRI.member.matrix <- PRI.member.matrix[,-bad.vote]
PRD.member.matrix <- PRD.member.matrix[,-bad.vote]
PAN.member.matrix <- PAN.member.matrix[,-bad.vote]
PAN.year.vote.matrix <- PAN.year.vote.matrix[,-bad.vote]
PRI.year.vote.matrix <- PRI.year.vote.matrix[,-bad.vote]
PRD.year.vote.matrix <- PRD.year.vote.matrix[,-bad.vote]

PAN.commPres.matrix <- PAN.commPres.matrix[,-bad.vote] 
PRI.commPres.matrix <- PRI.commPres.matrix[,-bad.vote] 
PRD.commPres.matrix <- PRD.commPres.matrix[,-bad.vote]
PAN.cabinet.matrix <- PAN.cabinet.matrix[,-bad.vote]
PRI.cabinet.matrix <- PRI.cabinet.matrix[,-bad.vote]
PRD.cabinet.matrix <- PRD.cabinet.matrix[,-bad.vote]
PAN.state.muni.leg.matrix <- PAN.state.muni.leg.matrix[,-bad.vote]
PRI.state.muni.leg.matrix <- PRI.state.muni.leg.matrix[,-bad.vote]
PRD.state.muni.leg.matrix <- PRD.state.muni.leg.matrix[,-bad.vote]
PAN.exec.exp.matrix <- PAN.exec.exp.matrix[,-bad.vote]
PRI.exec.exp.matrix <- PRI.exec.exp.matrix[,-bad.vote]
PRD.exec.exp.matrix <- PRD.exec.exp.matrix[,-bad.vote]

PAN.fem.matrix <- PAN.fem.matrix[,-bad.vote]
PAN.male.matrix <- PAN.male.matrix[,-bad.vote]
PRI.fem.matrix <- PRI.fem.matrix[,-bad.vote]
PRI.male.matrix <- PRI.male.matrix[,-bad.vote]
PRD.fem.matrix <- PRD.fem.matrix[,-bad.vote]
PRD.male.matrix <- PRD.male.matrix[,-bad.vote]
female.matrix  <- female.matrix[,-bad.vote]

PAN.poverty.matrix <- PAN.poverty.matrix[,-bad.vote]
PRI.poverty.matrix <- PRI.poverty.matrix[,-bad.vote]
PRD.poverty.matrix <- PRD.poverty.matrix[,-bad.vote]
PAN.victory.margin.matrix <- PAN.victory.margin.matrix[,-bad.vote]
PRI.victory.margin.matrix <- PRI.victory.margin.matrix[,-bad.vote]
PRD.victory.margin.matrix <- PRD.victory.margin.matrix[,-bad.vote]
PAN.competitorID.matrix <- PAN.competitorID.matrix[,-bad.vote]
PRI.competitorID.matrix <- PRI.competitorID.matrix[,-bad.vote]
PRD.competitorID.matrix <- PRD.competitorID.matrix[,-bad.vote]
PAN.year1.vote.matrix <- PAN.year1.vote.matrix[,-bad.vote]
PAN.year2.vote.matrix <- PAN.year2.vote.matrix[,-bad.vote]
PAN.year3.vote.matrix <- PAN.year3.vote.matrix[,-bad.vote]
PRI.year1.vote.matrix <- PRI.year1.vote.matrix[,-bad.vote]
PRI.year2.vote.matrix <- PRI.year2.vote.matrix[,-bad.vote]
PRI.year3.vote.matrix <- PRI.year3.vote.matrix[,-bad.vote]
PRD.year1.vote.matrix <- PRD.year1.vote.matrix[,-bad.vote]
PRD.year2.vote.matrix <- PRD.year2.vote.matrix[,-bad.vote]
PRD.year3.vote.matrix <- PRD.year3.vote.matrix[,-bad.vote]

PAN.pcbrequest.matrix <- PAN.pcbrequest.matrix[,-bad.vote]
PRI.pcbrequest.matrix <- PRI.pcbrequest.matrix[,-bad.vote]
PRD.pcbrequest.matrix <- PRD.pcbrequest.matrix[,-bad.vote]
PAN.prezposition.matrix <- PAN.prezposition.matrix[,-bad.vote]
PRI.prezposition.matrix <- PRI.prezposition.matrix[,-bad.vote]
PRD.prezposition.matrix <- PRD.prezposition.matrix[,-bad.vote]
PAN.cabposition.matrix <- PAN.cabposition.matrix[,-bad.vote]
PRI.cabposition.matrix <- PRI.cabposition.matrix[,-bad.vote]
PRD.cabposition.matrix <- PRD.cabposition.matrix[,-bad.vote]
PAN.proceduralvote.matrix <- PAN.proceduralvote.matrix[,-bad.vote]
PRI.proceduralvote.matrix <- PRI.proceduralvote.matrix[,-bad.vote]
PRD.proceduralvote.matrix <- PRD.proceduralvote.matrix[,-bad.vote]

Year.1.vote.matrix <- Year.1.vote.matrix[,-bad.vote]
Year.2.vote.matrix <- Year.2.vote.matrix[,-bad.vote]
Year.3.vote.matrix <- Year.3.vote.matrix[,-bad.vote]
committeeChair.matrix <- committeeChair[,-bad.vote] 
stateLegislator.matrix <- stateLegislator[,-bad.vote]
PCB.matrix <- PCB[,-bad.vote]
PrezPosition.matrix <- PrezPosition[,-bad.vote]
Year.matrix <- Year[,-bad.vote]
Poverty.matrix <- Income[,-bad.vote]
PR.matrix <- PR[,-bad.vote]

victoryMargin    <- victoryMargin[,-bad.vote]
victoryMarginDummy <- victoryMarginDummy[,-bad.vote]
proceduralVote   <- proceduralVote[,-bad.vote]
cabinetExp  <- cabinetExp[,-bad.vote]
executiveExp     <- executiveExp[,-bad.vote]
substituteLegs   <- substituteLegs[,-bad.vote]

MexBill61<- MexBill61[-grep("V2.3.2", MexBill61$VoteNumber),]


PCB.request <- MexBill61$PCB.request
President.position <- MexBill61$President.position
Cabinet.position <- MexBill61$Cabinet.position
Procedural.vote <- MexBill61$Procedural.vote
PRILeaderDecision<- MexBill61$PRILeaderDecision
PRI.hi.std.LeaderDecision<- MexBill61$PRI.hi.std.LeaderDecision
PANLeaderDecision<- MexBill61$PANLeaderDecision
PAN.hi.std.LeaderDecision<- MexBill61$PAN.hi.std.LeaderDecision
PRDLeaderDecision<- MexBill61$PRDLeaderDecision
PRD.hi.std.LeaderDecision<- MexBill61$PRD.hi.std.LeaderDecision



# Calculate how majority of the party votes
unity.index.function <- function (tmp) {
   no.Aye <- length (tmp[tmp==1 & !is.na(tmp)])
   no.Nay <- length (tmp[tmp==0 & !is.na(tmp)])
   tmp <- ifelse (no.Aye > no.Nay, 1, 0)
   return (tmp)
}

PRI.majority <- PAN.majority <- PRD.majority <- c()
for (j in 1:ncol(full.roll.call)){
   PRI.majority[j] <- unity.index.function (full.roll.call[!is.na(full.party.membership[,j]) & full.party.membership[,j]==1,j])
   PAN.majority[j] <- unity.index.function (full.roll.call[!is.na(full.party.membership[,j]) & full.party.membership[,j]==2,j])
   PRD.majority[j] <- unity.index.function (full.roll.call[!is.na(full.party.membership[,j]) & full.party.membership[,j]==3,j])
}


# Majorities in favor of votes V81.2.1, V66.3.1, V92.3.1
round (PRI.majority[c(89,150,174)]*100)
round (PAN.majority[c(89,150,174)]*100)
round (PRD.majority[c(89,150,174)]*100)

# Calculate proportion of party members that abstain
percent.abstention <- function (tmp) {
  no.Abs <- length(tmp[is.na(tmp)])
  obj <- no.Abs/length(tmp)
  return (obj)
}

PRI.pcnt.abstain <- PAN.pcnt.abstain <- PRD.pcnt.abstain <- c()
for (j in 1:ncol(full.roll.call)){
  PRI.pcnt.abstain[j] <- percent.abstention (full.roll.call[!is.na(full.party.membership[,j]) & full.party.membership[,j]==1,j])
  PAN.pcnt.abstain[j] <- percent.abstention (full.roll.call[!is.na(full.party.membership[,j]) & full.party.membership[,j]==2,j])
  PRD.pcnt.abstain[j] <- percent.abstention (full.roll.call[!is.na(full.party.membership[,j]) & full.party.membership[,j]==3,j])
}

# Calculate majority size
majority.size.function <- function (tmp) {
   no.Aye <- length (tmp[tmp==1 & !is.na(tmp)])
   no.Nay <- length (tmp[tmp==0 & !is.na(tmp)])
   tmp <- no.Aye / (no.Aye + no.Nay)
   return (tmp)
}

PRI.majority.size <- PAN.majority.size <- PRD.majority.size <- c()
for (j in 1:ncol(full.roll.call)){
   PRI.majority.size[j] <- majority.size.function (full.roll.call[!is.na(full.party.membership[,j]) & full.party.membership[,j]==1,j])
   PAN.majority.size[j] <- majority.size.function (full.roll.call[!is.na(full.party.membership[,j]) & full.party.membership[,j]==2,j])
   PRD.majority.size[j] <- majority.size.function (full.roll.call[!is.na(full.party.membership[,j]) & full.party.membership[,j]==3,j])
}

# Majorities in favor of votes V81.2.1, V66.3.1, V92.3.1
round (PRI.majority.size[c(89,150,174)]*100)
round (PAN.majority.size[c(89,150,174)]*100)
round (PRD.majority.size[c(89,150,174)]*100)

round (min (0.5 + abs (PRI.majority.size - 0.5)), 2)
round (mean (0.5 + abs (PRI.majority.size - 0.5)), 2)
round (mean (PRI.pcnt.abstain), 2)

round (min (0.5 + abs (PAN.majority.size - 0.5)), 2)
round (mean (0.5 + abs (PAN.majority.size - 0.5)), 2)
round (mean (PAN.pcnt.abstain), 2)

round (min (0.5 + abs (PRD.majority.size - 0.5)), 2)
round (mean (0.5 + abs (PRD.majority.size - 0.5)), 2)
round (mean (PRD.pcnt.abstain), 2)

objects2keep <- list(PRILeaderDecision, PANLeaderDecision, PRDLeaderDecision)

## Import pressure data
load ("mexico_ptirt_mcmc.Rda")

X.e   <- rbind (chainsPTIRT[[1]][,grep("X",  colnames (chainsPTIRT[[1]]))],
                chainsPTIRT[[2]][,grep("X",  colnames (chainsPTIRT[[2]]))])

rm (chainsPTIRT)

X.pri <- X.e[,grep(",1]", colnames(X.e))]
X.pan <- X.e[,grep(",2]", colnames(X.e))]
X.prd <- X.e[,grep(",3]", colnames(X.e))]

# Uncomment next line to save file
# save.image ("prepared_roll_call_data_mexicoTEST.Rdata")


